package com.ruoyi.web.controller.inv;

import com.ruoyi.buy.domain.entity.PO.InvBackboundBill;
import com.ruoyi.buy.domain.entity.PO.InvBackboundBillItem;
import com.ruoyi.buy.domain.entity.PO.InvOutboundBillItem;
import com.ruoyi.buy.domain.entity.inv0005.Inv0005InvDataDto;
import com.ruoyi.buy.domain.entity.inv0005.Inv0005UpdateByIdVO;
import com.ruoyi.buy.domain.entity.inv0008.Inv0008OutInvDataDto;
import com.ruoyi.buy.domain.entity.inv0008.Inv0008UpdateByIdVO;
import com.ruoyi.buy.service.IInvBackboundBillService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 退库单Controller
 * 
 * @author ruoyi
 * @date 2024-01-03
 */
@RestController
@RequestMapping("/inv/backbill")
public class InvBackboundBillController extends BaseController
{
    @Autowired
    private IInvBackboundBillService invBackboundBillService;

    /**
     * 查询退库单列表
     */

    @GetMapping("/list")
    public TableDataInfo list(InvBackboundBill invBackboundBill)
    {
        startPage();
        List<InvBackboundBill> list = invBackboundBillService.selectInvBackboundBillList(invBackboundBill);
        return getDataTable(list);
    }

    /**
     * 导出退库单列表
     */

    @Log(title = "退库单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InvBackboundBill invBackboundBill)
    {
        List<InvBackboundBill> list = invBackboundBillService.selectInvBackboundBillList(invBackboundBill);
        ExcelUtil<InvBackboundBill> util = new ExcelUtil<InvBackboundBill>(InvBackboundBill.class);
        util.exportExcel(response, list, "退库单数据");
    }

    /**
     * 获取退库单详细信息
     */

    @PostMapping(value = "/getInfo")
    public AjaxResult getInfo(@RequestBody InvBackboundBill invBackboundBill)
    {
        return success(invBackboundBillService.selectInvBackboundBillById(invBackboundBill.getId()));
    }

    /**
     * 新增退库单
     */

    @Log(title = "退库单", businessType = BusinessType.INSERT)
    @PostMapping("/addBackbill")
    public AjaxResult add(@RequestBody Inv0008UpdateByIdVO para)
    {
        return toAjax(invBackboundBillService.insertInvBackboundBill(para));
    }

    /**
     * 修改退库单
     */

    @Log(title = "退库单", businessType = BusinessType.UPDATE)
    @PostMapping("/updateBackbill")
    public AjaxResult edit(@RequestBody Inv0008UpdateByIdVO para)
    {
        return toAjax(invBackboundBillService.updateInvBackboundBill(para));
    }

    /**
     * 删除退库单
     */

    @Log(title = "退库单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(invBackboundBillService.deleteInvBackboundBillByIds(ids));
    }

    @PostMapping("/getOutInvData")
    public TableDataInfo getOutInvData(@RequestBody Inv0005InvDataDto para)
    {
        startPage();
        List<Inv0008OutInvDataDto> list = invBackboundBillService.getOutInvData(para);
        return getDataTable(list);
    }

    /**
     * 删除出库单明细
     */
    @Log(title = "退库单明细", businessType = BusinessType.DELETE)
    @PostMapping("/delItem")
    public AjaxResult delItem(@RequestBody InvBackboundBillItem para)
    {
        return toAjax(invBackboundBillService.deleteInvBackboundBillItemById(para));
    }

    @PostMapping("/checkInvQty")
    public AjaxResult checkInvQty(@RequestBody Inv0005InvDataDto para)
    {
        String message = invBackboundBillService.checkInvQty(para);
        if(("成功").equals(message)){
            return success("成功");
        }else{
            return error(message);
        }
    }

    @PostMapping("/executeBack")
    public AjaxResult executeOut(@RequestBody Inv0008UpdateByIdVO para) throws Exception {
        String message =invBackboundBillService.executeBack(para);
        if(("成功").equals(message)){
            return success("成功");
        }else{
            return error(message);
        }
    }
}
